import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入组件
import Login from '@/views/Login'
import Home from '@/views/Home'
import Welcome from '@/views/Welcome'
import Cate from '@/views/Cate'
import CateAdd from '@/views/CateAdd'
import CateEdit from '@/views/CateEdit'
import Content from '@/views/Content'
import ContentAdd from '@/views/ContentAdd'
import ContentEdit from '@/views/ContentEdit'
import Adv from '@/views/Adv'
import AdvContent from '@/views/AdvContent'
import AdvContentAdd from '@/views/AdvContentAdd'
import AdvContentEdit from '@/views/AdvContentEdit'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '/', component: Welcome },
      { path: 'cate', component: Cate },
      { path: 'cate/add', component: CateAdd },
      { path: 'cate/edit/:id', component: CateEdit },
      { path: 'content', component: Content },
      { path: 'content/add', component: ContentAdd },
      { path: 'content/edit/:id', component: ContentEdit },
      { path: 'adv', component: Adv },
      { path: 'adv/content', component: AdvContent },
      { path: 'adv/content/add', component: AdvContentAdd },
      { path: 'adv/content/edit/:id', component: AdvContentEdit }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 过载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  // 判断 tokenStr 是否为空
  if (!tokenStr) return next('/login')
  next()
})

export default router
